package com.horsen.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.horsen.common.util.R;
import com.horsen.service.edu.controller.BaseController;
import com.horsen.service.edu.model.Course;
import com.horsen.service.edu.model.DTO.CourseInsertDTO;
import com.horsen.service.edu.model.DTO.CourseQueryDTO;
import com.horsen.service.edu.model.VO.CourseInfoVO;
import com.horsen.service.edu.model.VO.CourseListVO;
import com.horsen.service.edu.model.VO.CoursePublishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 课程 控制器
 *
 * @author Horsen
 */
@Api(tags = "课程管理")
@ApiSupport(order = 4)
@RestController("AdminCourse")
@RequestMapping("/admin/edu/courses")
public class CourseController extends BaseController {

    @ApiOperationSupport(order = 1)
    @ApiOperation("分页获取课程列表")
    @GetMapping("/{page}/{limit}")
    public R queryByPage(@ApiParam(value = "当前页码", required = true)
                         @PathVariable Long page,
                         @ApiParam(value = "每页记录数", required = true)
                         @PathVariable Long limit,
                         @ApiParam("讲师查询对象") CourseQueryDTO courseQueryDTO) {
        IPage<CourseListVO> pageParam = courseService.pageSelect(page, limit, courseQueryDTO);
        List<CourseListVO> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return list != null ?
                R.ok("分页获取所有课程成功").setData("items", list).setData("total", total) : R.error("分页获取所有课程失败");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("新增一个课程")
    @PostMapping("/")
    public R insert(@ApiParam(value = "课程对象", required = true)
                    @RequestBody CourseInsertDTO courseInsertDTO) {
        String id = courseService.insert(courseInsertDTO);
        return id != null ?
                R.ok("新增课程成功").setData("id", id) : R.error("新增课程失败");
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("根据ID获取一个课程信息")
    @GetMapping("/info/{id}")
    public R queryInfoById(@ApiParam(value = "课程ID", required = true)
                           @PathVariable String id) {
        CourseInfoVO courseInfoVO = courseService.queryInfoById(id);
        return courseInfoVO != null ?
                R.ok("获取ID=" + id + "的课程信息成功").setData("item", courseInfoVO)
                : R.error("获取ID=" + id + "的课程信息失败");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("修改一个课程")
    @PutMapping("/")
    public R update(@ApiParam("课程对象")
                    @RequestBody CourseInfoVO courseInfoVO) {
        return courseService.updateById(courseInfoVO) ?
                R.ok("修改课程成功") : R.error("修改课程失败");
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("根据ID删除课程")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam(value = "课程ID", required = true)
                        @PathVariable String id) {
        return courseService.deleteById(id) ?
                R.ok("删除ID=" + id + "的课程成功") : R.error("删除ID=" + id + "的课程失败");
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("根据ID获取一个课程发布")
    @GetMapping("/publish/{id}")
    public R queryPublishById(@ApiParam(value = "课程ID", required = true)
                              @PathVariable String id) {
        CoursePublishVO coursePublishVO = courseService.getPublishById(id);
        return coursePublishVO != null ?
                R.ok("获取ID=" + id + "的课程发布成功").setData("item", coursePublishVO)
                : R.error("获取ID=" + id + "的课程发布失败");
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("根据ID修改课程状态")
    @PutMapping("/{id}/{status}")
    public R updateStatus(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id,
            @ApiParam(value = "状态", required = true)
            @PathVariable String status) {
        Course course = courseService.getById(id);
        if (StringUtils.equals(status, Course.COURSE_STATE_FAIL)) {
            course.setStatus(Course.COURSE_STATE_FAIL);
            courseService.updateById(course);
            return R.ok("下架课程成功");
        }
        course.setStatus(Course.COURSE_STATE_PASS);
        courseService.updateById(course);
        return R.ok("发布课程成功");
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation("获取课程数")
    @GetMapping("/count")
    public R count() {
        Integer count = courseService.count();
        return R.ok("获取课程数成功").setData("count", count);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("获取课程柱状图数据")
    @GetMapping("/chart")
    public R chart() {
        Map<String, Object> courseChart = courseService.getChartValue();
        return R.ok("获取课程柱状图数据成功").setData("courseChart", courseChart);
    }
}

